package com.qf.controller;


import com.qf.entity.Coupon;
import com.qf.entity.CouponTemplate;
import com.qf.feign.CouponFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 优惠券相关的调用
 */
@Controller
@RequestMapping("/system/coupon")
public class SystemCouponController  {

    @Autowired
    private CouponFeign couponFeign;

    /**
     * 跳转到模板列表页
     * @return
     */
    @RequestMapping("/templatelist")
    public String templateList(Model model){
        List<CouponTemplate> templates = couponFeign.list().getData();
        model.addAttribute("templates", templates);
        return "coupontemplatelist";
    }

    @ResponseBody
    @RequestMapping("/templatelistajax")
    public List<CouponTemplate> templateListAjax(){
        List<CouponTemplate> templates = couponFeign.list().getData();
        return templates;
    }

    /**
     * 新增模板
     * @return
     */
    @RequestMapping("/templateinsert")
    public String templateAdd(CouponTemplate couponTemplate){
        couponFeign.save(couponTemplate);
        return "redirect:/system/coupon/templatelist";
    }

    /**
     * 优惠券列表
     * @return
     */
    @RequestMapping("/couponlist")
    public String couponList(Model model){
        //调用优惠券服务查询优惠券列表
        List<Coupon> couponlist = couponFeign.couponlist().getData();
        model.addAttribute("coupons", couponlist);
        return "couponlist";
    }

    /**
     * 新增优惠券
     * @return
     */
    @RequestMapping("/couponadd")
    public String couponadd(Coupon coupon){
        System.out.println("接收到提交优惠券的对象：" + coupon);
        couponFeign.couponSave(coupon);
        return "redirect:/system/coupon/couponlist";
    }
}